/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Util.Utilitarioxml;

import Entidades.Address;
import Entidades.Client;
import Entidades.Hardware;
import Entidades.Item;
import Entidades.Orderes;
import Entidades.Product;
import Entidades.Software;
import Entidades.Supply;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author Patricio-Pc
 */
public class XMLmarshalling {

    /**
     * baseado em ordem cliente
     */
    public static File gerarXML(Orderes ordem) {

        Element root = new Element("pedido");
        if (ordem.getID() != null) {
            root.setAttribute("id", String.valueOf(ordem.getID()));
        }

        root.setAttribute("number", String.valueOf(ordem.getNumber()));

        Element cliente = objClienteForElement(ordem.getClient());
        root.setContent(cliente);

         for (Item item1 : ordem.getItem()) {
            Element itemdeLinha = new Element("itemdeLinha");
            itemdeLinha.setAttribute("quantity",String.valueOf(item1.getQuanttity()));
            
            Element product =   ProductoForElemente(item1.getProduct());
            itemdeLinha.setContent(product);
            
            root.setContent(itemdeLinha);
        }
    
        Document doc = new Document(root);
        File file = new File("RespostaPedido" + ordem.getNumber());
        Writer out;
        try {
            out = new OutputStreamWriter(new FileOutputStream(file));
            XMLOutputter xout = new XMLOutputter();
            xout.setFormat(Format.getCompactFormat().setEncoding("ISO-8859-1"));
            xout.output(doc, out);
        } catch (IOException ex) {
            Logger.getLogger(XMLmarshalling.class.getName()).log(Level.SEVERE, null, ex);
        }

        return file;
    }

    private static Element objClienteForElement(Client client) {
        Element cliente = new Element("cliente");
        cliente.setAttribute("custumerID", client.getId().toString());
        cliente.setAttribute("name", client.getName());

        Element enderecliente = endrecoForElemente(client.getAddress());

        cliente.setContent(enderecliente);

        return cliente;
    }

    private static Element endrecoForElemente(Address address) {
        Element endereco = new Element("endereco");

        endereco.setAttribute("street", address.getStreet());
        endereco.setAttribute("city", address.getCity());
        endereco.setAttribute("stateProvince", address.getStateORprovince());
        endereco.setAttribute("postalCode", address.getPostalCode());
        endereco.setAttribute("country", address.getContry());

        return endereco;

    }

    private static Element ProductoForElemente(Product product) {
       Element producto = new Element("produto");
     
       producto.setAttribute("description",product.getDescription());
       producto.setAttribute("unitPrice",product.getPrice().toString());
       
        if (product instanceof  Hardware) {
            producto.setAttribute("type","hardware");
            producto.setAttribute("assemby",((Hardware) product).getAssembly().toString());
        }else{
            producto.setAttribute("type","hardware");
            producto.setAttribute("version",((Software) product).getVersion().toString());
            }
            
       Element elementSupply = SupplyForElemente(product.getSupply());
       producto.addContent(elementSupply);   
       
       return producto;
    }
    /*
    public static void main(String[] args) {
        Product p = new Hardware();
        
    }*/

    private static Element SupplyForElemente(Supply supply) {
        Element SupplyElement = new Element("fornecedor");
        SupplyElement.setAttribute("number",supply.getNumer().toString());
        Element addresElement = endrecoForElemente(supply.getAddress());
        SupplyElement.addContent(addresElement);
        
        return SupplyElement;
    }
}
